// 好友申请弹窗
import 'package:flutter/material.dart';

class DialogWithInput extends StatefulWidget {
  final String nickname;
  final ValueChanged<String> onSubmit;
  final String initMessage;
  final String title;
  final String subtitle;

  const DialogWithInput({
    super.key,
    required this.nickname,
    required this.onSubmit,
    required this.initMessage,
    required this.title,
    required this.subtitle,
  });

  @override
  State<DialogWithInput> createState() => _DialogWithInputState();
}

class _DialogWithInputState extends State<DialogWithInput> {
  @override
  void initState() {
    super.initState();
    setState(() {
      _controller.text = widget.initMessage;
    });
  }

  final _controller = TextEditingController();
  final _accentColor = const Color(0xFF99A8F1);

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildHeader(),
            const SizedBox(height: 16),
            _buildInputField(),
            const SizedBox(height: 24),
            _buildActionButtons(),
          ],
        ),
      ),
    );
  }

  Widget _buildHeader() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          widget.title,
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.w600,
            color: Colors.grey[800],
          ),
        ),
        const SizedBox(height: 4),
        Text(
          '${widget.subtitle}: ${widget.nickname}',
          style: TextStyle(color: Colors.grey[600], fontSize: 14),
        ),
      ],
    );
  }

  Widget _buildInputField() {
    return TextField(
      controller: _controller,
      maxLines: 3,
      maxLength: 50,
      decoration: InputDecoration(
        hintText: '请输入信息',
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8),
          borderSide: BorderSide(color: Colors.grey[300]!),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8),
          borderSide: BorderSide(color: _accentColor),
        ),
        contentPadding: const EdgeInsets.all(16),
      ),
    );
  }

  Widget _buildActionButtons() {
    return Row(
      children: [
        Expanded(
          child: TextButton(
            style: TextButton.styleFrom(
              padding: const EdgeInsets.symmetric(vertical: 12),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6),
              ),
            ),
            onPressed: () => Navigator.pop(context),
            child: Text(
              '取消',
              style: TextStyle(color: Colors.grey[600], fontSize: 16),
            ),
          ),
        ),
        SizedBox(width: 16),
        Expanded(
          child: FilledButton(
            style: FilledButton.styleFrom(
              backgroundColor: _accentColor,
              padding: const EdgeInsets.symmetric(vertical: 12),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6),
              ),
            ),
            onPressed: () {
              widget.onSubmit(_controller.text.trim());
              Navigator.pop(context);
            },
            child: const Text(
              '发送',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
          ),
        ),
      ],
    );
  }
}
